import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.aliasi.classify.Classification;
import com.aliasi.classify.Classified;
import com.aliasi.classify.ConfusionMatrix;
import com.aliasi.classify.JointClassifierEvaluator;
import com.aliasi.classify.TradNaiveBayesClassifier;
import com.aliasi.tokenizer.EnglishStopTokenizerFactory;
import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;
import com.aliasi.tokenizer.LowerCaseTokenizerFactory;
import com.aliasi.tokenizer.TokenizerFactory;



/*@Sangeeta
 *@Uses: Program to calculate probabilities using naive baysian.
 *@Task:
 *1. Read File.
 *2. Clean it for<> and &nbspace.
 *3. Tokeinze its content.
 *4. 
 *@To be done
 *1. getclasses function can be different for TRAINING and TESTING.
 *2. Check for category prior and token Prior.
 *3. Replace Training and testing tables with desired tables.
 */

public class ProbCalculation 
{
	public String dbname="jboss";
	public String user="root";
	public String pass="123";
	public static Connection conn=null;
	public Set<String> classes=null;
	IndoEuropeanTokenizerFactory ietf = IndoEuropeanTokenizerFactory.INSTANCE;
	TokenizerFactory f2=new LowerCaseTokenizerFactory(ietf);
	EnglishStopTokenizerFactory tokFact=new EnglishStopTokenizerFactory(f2); 
	//public double catPrior = 0.1;
	//public double tokenPrior = 0.0005;
	public double lengthNorm = Double.NaN;//No length Normalization yet
	public TradNaiveBayesClassifier classifier;
	public ConfusionMatrix cm;
	public String class_array[];
	

	public ArrayList<String> stopped=new ArrayList<String>();
	
	//1. Read files description
	//2. clean
	//3. Tokenize
	//4.

public void trainStop ()
{
		//Read all the classes from data base
		classes=getClasses();		
		//classifier=new TradNaiveBayesClassifier (classes,tokFact,catPrior,tokenPrior,lengthNorm);
		classifier=new TradNaiveBayesClassifier (classes,tokFact);
		
		
		Object[] temp=classes.toArray();
		class_array= new String[temp.length];
		
		//for() each file get the content of all the related bugids
		
		for(int i=0;i<temp.length;i++)
		{
			class_array[i]=temp[i].toString();
			//System.out.println("class array"+class_array[i]);
		}		
		
		
	  for(int i=0;i<classes.size();i++)
		{
			String current_cat = class_array[i];
			Classification current_class=new Classification(current_cat);
			
			
			 try
			 {
				 Statement stmt=null;
				 stmt=conn.createStatement();
				 
				 String q1="select distinct bugid from top10_nfid_bugid where nfid='"+current_cat+"'";
				 stmt.execute(q1);
				 ResultSet rs=stmt.getResultSet();
				 //System.out.println("rs len="+rs.getFetchSize());
				 while(rs.next())
				 {
					 String bugid=rs.getString("bugid");
					 System.out.println("bugid = "+bugid);
					 String content  = ReadBugDescTitle(bugid);
					 general_fun gf =  new general_fun();
					 content=gf.clean(content);					 
					 content = gf.remove_stop_words(content);
					 content=gf.remove_SW(content);		
					 //content=gf.remove_one_letter(content);
					 
					 System.out.println("Content="+content);
					 classifier.handle(new Classified<CharSequence>(content, current_class));
					 //displayTokens(content,ietf);					 
					 System.out.println("print");
				 }//wh
			
				 
				 System.out.println("I am here");
				 if(rs!=null)
					 rs.close();
				 
				 if(stmt!=null)
				 {stmt.close();}
				 
				 System.out.println("I am here");
				 
			 }catch(SQLException e)
			 {
				 e.printStackTrace();
			 }	
		
		}//for
		
}//func
	
public void displayTokProb()throws IOException
{
	Statement stmt =  null;
	System.out.println("Displaing probablities of classes\n------------------\n");
	double sum=0;
	for(int i=0;i<classes.size();i++)
	{
		double classProb=classifier.probCat(class_array[i]);
		System.out.println(class_array[i]+"= "+classProb);
		sum += classProb;
	}
	System.out.println("\n-------------------------sum:"+sum);
	
	Vector<String> fname_vect = get_file_names(class_array);
	
	System.out.println("Displaying probablities of Tokens WRT to each category\n---n");
	

	try 
	{
		   
		stmt =  conn.createStatement();
		
	} catch (SQLException e) 
	{ e.printStackTrace(); 	}
	
	for(int i=0;i<class_array.length;i++)
		{
		   for(String tok:classifier.knownTokenSet() )
		   {
			 
			  // System.out.println("Tok="+ tok+" "+ "  prob="+classifier.probToken(tok,class_array[i])*10000);
			   String nfid = class_array[i];
			   String fname = fname_vect.get(i);
			   double tok_prob = classifier.probToken(tok, class_array[i])*10000;
			   
			   String q ="insert into top10_tok_prob values('"+nfid+"','"+tok+"','"+fname+"',"+tok_prob+")";
			   System.out.println(q);
			   
			   //if(nfid.equalsIgnoreCase("nfid396") )
			   //{
				  //intr();
			   //}			  
			   
			   try 
			   {
				 stmt.executeUpdate(q);
			   
			   } catch (SQLException e) 
			   { 	e.printStackTrace(); }
			 
		   }//for
		 
		}//forarg0
	
	
	try 
	{
			if(stmt!=null)
				stmt.close();
			
	} catch (SQLException e) 
	{ e.printStackTrace(); 	}
	
	/*for(String tok:classifier.knownTokenSet())
		{
			String query="Insert into top1o_tok_prob values ('"+tok.trim()+"'";
			//out.println(tok);
			
			for(int j=0;j<class_array.length;j++)
			{
				double tokProb=classifier.probToken(tok, class_array[j])*10000;
				query =query+ ",'"+class_array[j]+"','"+tokProb+"'";
			}
			
			//System.out.println(query);
			Statement stmt1;
			try 
			   {
					stmt1 = conn.createStatement();
					//stmt1.executeUpdate(query);
					stmt1.close();
				} catch (SQLException e) 
				 { 	e.printStackTrace(); }
			
			out.println("---------");
			
		}*/
	
	
	
	System.out.println("Known token set length="+classifier.knownTokenSet().size());
	
	
}//display

private Vector<String> get_file_names(String class_array[]) 
{
  
  Statement stmt=null;
 
   String q1 = "select distinct nfid from top10_nfid_bugid";
   
   Vector<String> nfid =  new Vector<String>();
   Vector<String> fname = new Vector<String>();
   
   try 
   {
	   stmt = conn.createStatement();
	   stmt.execute(q1);
	   
	   ResultSet r1 =  stmt.getResultSet();
	   while(r1.next())
	   	{
		   String nfid_temp = r1.getString("nfid");
		   nfid.add(nfid_temp);
	   	}
	   
	   
	   for(int i=0;i<nfid.size();i++)
	   {
		   String nfid_str = nfid.get(i);
          
		   String q2 = "select fname from n_fname_fdir where nfid='"+nfid_str+"'";
		   stmt.execute(q2);
		   r1 = stmt.getResultSet();
		   
		   while(r1.next())
		   {
			   String fname_str =  r1.getString("fname");
			   fname.add(fname_str);
			   
		   }

	   }
	   
	   
	   if(r1!=null)
	   { r1.close();}
	   
	   if(stmt!=null)
	   	{stmt.close();}
	   
	   
	   
	   
   	} catch (SQLException e) 
   	{ e.printStackTrace();}
   
 
   	return fname;
	
}

//getclasses
public Set getClasses()
 { 
		Set classes=null;
		classes= new HashSet();
		
		Statement stmt=null;
		try {
				stmt=conn.createStatement();

				String query="select * from top10_nfid_bugid";

				//System.out.println("Class query="+query);
				stmt.execute(query);
				ResultSet rs=stmt.getResultSet();
				while(rs.next())
					{
						String newClass=rs.getString("nfid");
						classes.add(newClass);
					}
				
				
				if(rs!=null)
						rs.close();
				
				if(stmt!=null)
					stmt.close();
				
				/*
				//print class names
				Object[] cat=classes.toArray();
				for(int i=0;i<cat.length;i++)
					System.out.println("Cat="+cat[i].toString());*/
					
		
			} catch (SQLException e)
				{	e.printStackTrace();}
			
		return classes;
}
	

public String ReadBugDescTitle(String bugid)
{
		//bugid="JBAS-2";
		String descTitle="";
		
		Statement stmt =  null;
		
		try {
					
				stmt=conn.createStatement();		
				String query ="select description from bug_svn_info where bugid='"+bugid+"'";
		
		 
				stmt.execute(query);
				ResultSet rs=stmt.getResultSet();
				while(rs.next())
				{
					descTitle=rs.getString("description");
					//System.out.println("Description"+desc);
				}
				
				stmt=conn.createStatement();		
				query ="select summary from bug_svn_info where bugid='"+bugid+"'";
		
		 
				stmt.execute(query);
				rs=stmt.getResultSet();
				while(rs.next())
				{
					descTitle=descTitle +" "+ rs.getString("summary");
					//System.out.println("Description"+desc);
				}
				
			} catch (SQLException e) 
				{ e.printStackTrace(); 	}
		
		return descTitle;
}
		
// 
public static void main(String args[])throws IOException
   {
	   ProbCalculation pc= new ProbCalculation();
	   
	  try
	  	{
		  db_conn db =  new db_conn();
		  conn= db.connect(pc.dbname, pc.user, pc.pass);
		  pc.trainStop();
		  //pc.train();
		  
		  System.out.println("Trained....");

		  
		  pc.displayTokProb();
		
		  System.out.println("Testing....");		  
		 // pc.test();// Not working currently
		 
		  
		  if(conn!=null)
		  {
			  conn.close();
		  }
		  
	  	}catch(SQLException e)
	  	{
	  		e.printStackTrace();
	  	}
	   
	  
   }//main
 
/*
public static void intr()
 {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));	
    System.out.println("see");
    try 
    {
    	br.readLine();
    	} catch (IOException e) 
    	{ 	e.printStackTrace();  }
    	
 }//intr
*/

/*
//test
public void test()
{
  
boolean storeCategories = true;
JointClassifierEvaluator<CharSequence> evaluator 
         = new JointClassifierEvaluator<CharSequence>(classifier,
                                                      class_array,
                                                      storeCategories);
      
     for(int i=0;i<classes.size();i++)
		{
		
    	    String current_cat = class_array[i];
			//System.out.println("Test Current class="+i+" "+current_cat);
			Classification current_class=new Classification(current_cat);
					
			
			//1.Get all the bugids where file id is current class
			//2.Read their description
			//3.  
			 try
			 {
				 Statement stmt=null;
				 stmt=conn.createStatement();
				 
				 String q1="select * from fid_bugid where fid='"+current_cat+"'";
				// System.out.println("Test q1="+q1);
				 stmt.execute(q1);
				 ResultSet rs=stmt.getResultSet();
				 while(rs.next())
				 {
					 String bugid=rs.getString("bugid");
					 
					 					 
					 String content  = ReadBugDescTitle(bugid);
					 	 
					  
					 content=clean(content);
					 
					 Classification classification=classifier.classify(content);				 
					 String result=classification.bestCategory();
					 Classified<CharSequence> classified = new Classified<CharSequence>(content,classification); 
					 evaluator.handle(classified);
					 
					 System.out.println("Result="+result);
					 
				 }//wh
				  
				 if(stmt!=null)
				 {stmt.close();}
				 
			 }catch(SQLException e)
			 {
				 
			 }	
		
		}//for
		
    
     cm=evaluator.confusionMatrix();//confusion matrix
     double accuracy=cm.totalAccuracy();
     System.out.println("Accuracy="+accuracy);

}//test
*/



	
}//class
